<?php

/**
 * Description of TourbookController
 *
 * @author luigi.pastore
 * @created  27-lug-2012
 */
class TourbookController extends Classes_Controller
{
    public function init()
    {
        $this->model_tourbook       = new Classes_Tourbook;
        $this->model_tourbook_steps = new Classes_TourbookSteps;
	$this->model_live	    = new Classes_Live;
	$this->model_event	    = new Classes_Event;
	$this->model_event_types    = new Classes_EventType;
        $this->model_livestatus     = new Classes_LiveStatus;
        $this->model_province       = new Classes_Province();
        
	$this->event_types = $this->model_event_types->fetchAll();
	
	$this->layout->event_types = $this->event_types;

	// Edit...
	if( is_numeric( $id = $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $tourbook = $this->model_tourbook->fetchById( array( $id ) );

	    if( empty( $tourbook ) )
		trigger_error( 'Nessun Tourbook' );
	    
	    $this->tourbook = array_shift( $tourbook );
	    
            // Se sto editando precompilo la sessione
            if( !empty( $this->form_data['salva'] ) )
                $this->tourbook = array_merge( $this->tourbook, $this->form_data );
	    
	    // Se sto editando precompilo la sessione
	    if( !empty( $this->tourbook ) AND empty( $_SESSION['Tourbook'] ) )
	    {
		# Formattiamo campi
		// ...

//		$_SESSION['Event']['Info'] = $this->event;
//		$_SESSION['Event']['Live'] = $this->event['event_live'];
	    }	    
	}
        
        # Genera nuovo Tourbook
        if( $this->getAction() == 'elencolive' AND $this->getParam( 'id' ) == null )
        {
            $this->model_tourbook->tourbook_event_id = $this->getParam( 'event_id' );
            $this->model_tourbook->tourbook_createdby = $this->application->auth->user['user_id'];
            
            if( is_numeric( $tourbook_id = $this->model_tourbook->save()->tourbook_id ) )
            {
                // Redirect...
                return header( 'Location: /admin/tourbook/elencolive/id/' . $tourbook_id );
            }
            else
            {
                echo "<pre>";
                var_dump($this->model_tourbook->errors);
                echo "</pre>";
                exit;
            }
        }
      
        
        # Assegniamo i dati al layout
        $this->layout->data         = $this->tourbook;
	$this->layout->province     = $this->model_province->fetchAll();
        $this->layout->days         = $this->getResource( 'Days' );
        $this->layout->months       = $this->getResource( 'Months' );
        $this->layout->live_status  = $this->model_livestatus->fetchAll();
        
        // Dati Form
        $this->form_data = $this->getParams();
    }

    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'tourbook/index' );
    }

    public function elencoliveAction()
    {
	if( empty ( $this->tourbook ) )
	    trigger_error( 'Nessun Tourbook Selezionato' );

        $this->layout->count_events = $this->model_event->getCount();
	$this->layout->content = array( 'script' => 'tourbook/elencolive' );
    }
    
    public function compilaAction()
    {
	if( empty ( $this->tourbook ) )
	    trigger_error( 'Nessun Tourbook Selezionato' );
        
        // Edit...
	if( is_numeric( $step_id = $this->getParam( 'step' ) ) )
	{
	    // Fetch
	    $step = array_shift( $this->model_tourbook_steps->fetchById( array( $step_id ) ) );

	    if( empty( $step ) )
		trigger_error( 'Nessuno Step' );
	    
            // Se sto editando precompilo la sessione
            if( !empty( $this->form_data['salva'] ) )
                $step = array_merge( $step, $this->form_data );
	    
	    // Se sto editando precompilo la sessione
	    if( !empty( $this->tourbook ) AND empty( $_SESSION['Tourbook'] ) )
	    {
		# Formattiamo campi
		// ...

//		$_SESSION['Event']['Info'] = $this->event;
//		$_SESSION['Event']['Live'] = $this->event['event_live'];
	    }	    
	}

        $this->layout->data = $step;
	$this->layout->content = array( 'script' => 'tourbook/compila' );
    }
}